package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Create by keboom on 2020/6/22
 */
@Controller
public class LoginController {

    /**
     * 管理员登录，这里只有一个管理员，为了简单，直接就写在代码里了
     * @param adminname
     * @param adminpassword
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value = "/admin/login")
    public String login(@RequestParam("adminname") String adminname,
                        @RequestParam("adminpassword") String adminpassword,
                        Map<String, Object> map, HttpSession session) {
        if (!StringUtils.isEmpty(adminname) && "123456".equals(adminpassword)) {
            //登陆成功，防止表单重复提交，可以重定向到主页
            session.setAttribute("loginAdmin", adminname);
            return "redirect:main";
        } else {
            //登陆失败
            map.put("msg", "用户名密码错误");
            return "login";
        }
    }

    @GetMapping("/admin")
    public String login2(){
        return "login";
    }
}
